#include "StdAfx.h"
#include "sa_TwShader.h"


#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)




sa_TwShader::sa_TwShader(void)
{
}

sa_TwShader::~sa_TwShader(void)
{
}
void sa_TwShader::InitGraphics()
{
	LPD3DXBUFFER errorlog;    // for storing errors

	// load the effect file
	D3DXCreateEffectFromFile(g_Render->GetDevice(), (LPCSTR)"dxtshader.fx", 0, 0, 0, 0, &effect, &errorlog);

	// find the best technique
	effect->FindNextValidTechnique(NULL, &technique);
	D3DXCreateTeapot(g_Render->GetDevice(), &meshTeapot, NULL);    // create the teapot

	InitRenderState();

}

void sa_TwShader::InitRenderState()
{
	g_Render->GetDevice()->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer
}

void sa_TwShader::InitLight()
{
	D3DLIGHT9 light;
	ZeroMemory(&light, sizeof(light));

	light.Diffuse.r = light.Ambient.r = 0.3f;
	light.Diffuse.g = light.Ambient.g = 0.3f;
	light.Diffuse.b = light.Ambient.b = 0.3f;

	D3DVECTOR vecPosition = {2.0f, 2.0f, 2.0f};
	light.Position = vecPosition;

	effect->SetFloatArray("LightPos", &vecPosition.x, 3);
	effect->SetFloatArray("AmbientColor", &light.Ambient.r, 3);
	effect->SetFloatArray("DiffuseColor", &light.Diffuse.r, 3); 


}

void sa_TwShader::Update()
{

}

void sa_TwShader::Render()
{
	g_Render->GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(230, 230, 255), 1.0f, 0);
	g_Render->GetDevice()->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	g_Render->GetDevice()->BeginScene();

	// set the view transform
	D3DXMATRIX matView;    // the view transform matrix
	D3DXMatrixLookAtLH(&matView,
		&D3DXVECTOR3 (0.0f, 1.0f, -6.0f),    // the camera position
		&D3DXVECTOR3 (0.0f, 0.0f, 0.0f),    // the look-at position
		&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
	effect->SetMatrix("View", &matView);

	// set the projection transform
	D3DXMATRIX matProjection;    // the projection transform matrix
	D3DXMatrixPerspectiveFovLH(&matProjection,
		D3DXToRadian(45),    // the horizontal field of view
		(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
		1.0f,    // the near view-plane
		100.0f);    // the far view-plane
	effect->SetMatrix("Projection", &matProjection);

	// set the world transform
	static float index = 0.0f; index+=0.03f;    // an ever-increasing float value
	D3DXMATRIX matRotateY;    // a matrix to store the rotation for each triangle
	D3DXMatrixRotationY(&matRotateY, index);    // the rotation matrix
	effect->SetMatrix("World", &matRotateY);


	for(int count = -100; count < 0; count+=5)
	{
		static D3DXMATRIX matCopy;
		D3DXMatrixTranslation(&matCopy, 3.0f, -1.0f, (float)count + index);
		effect->SetMatrix("World", &matCopy);

		effect->Begin(NULL, NULL);    // begin using the effect
		effect->BeginPass(0);    // begin the pass

		// render whatever
		meshTeapot->DrawSubset(0);

		effect->EndPass();    // end the pass
		effect->End();    // end the effect
	}

	g_Render->GetDevice()->EndScene(); 

	g_Render->GetDevice()->Present(NULL, NULL, NULL, NULL);


}

void sa_TwShader::Clear()
{
	meshTeapot->Release();    // close and release the teapot mesh
	effect->Release();    // close and release the effect

}